define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init();

            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });

            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");


        },
        table:{
            record_all:function (){
                // 表格1
                var window_innerheight = window.innerHeight;
                var table_height = window_innerheight-75;
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'caiwu/record/index?type=0',
                    fixedColumns: true,
                    fixedNumber: 1,
                    pk: 'id',
                    extend: {
                        index_url: 'caiwu/record/index?type=0',
                        add_url: 'caiwu/record/add?type=0',
                        edit_url: 'caiwu/record/edit?type=0',
                        del_url: '',
                        multi_url: '',
                        table:'record_all'
                    },
                    pageSize: Config.pagesize,
                    height: table_height,
                    toolbar: '#toolbar1',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'),operate: false,visible: false},
                            {field: 'user_name', title: "操作人",operate: false},
                            {field: 'user_id', title: "选择用户",visible: false,addClass: "selectpage", extend: "data-source='caiwu/record/ajaxUser' data-field='name' data-page-size='20'",placeholder:"按照管理员名称可检索"},

                            {field: 'cate_bianhao', title: "分类编号",operate: false},
                            {field: 'cate_name', title: "分类名",operate: false},
                            {field: 'title', title: "标题",operate: false},
                            {field: 'price', title: "金额",operate: false},
                            {field: 'type', title: "类型",formatter: Controller.api.formatter.setType,operate: false},
                            {field: 'inventory_bianhao', title: "入库编号"},
                            {field: 'order_bianhao', title: "出库订单编号"},
                            {field: 'remark', title: "备注",operate: false},


                            {field: 'create_time', title: '创建时间',addclass:'datetimerange w200', operate:'RANGE', formatter: Table.api.formatter.datetime},
                            {field: 'update_time', title: '编辑时间', operate:false},
                            {field: 'operate', title: __('Operate'), table: table1, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);


            },
            shourushiji:function (){
                // 表格1
                var window_innerheight = window.innerHeight;
                var table_height = window_innerheight-75;
                var tableshiji = $("#tableshiji");
                tableshiji.bootstrapTable({
                    url: 'caiwu/record/index?type=1',
                    fixedColumns: true,
                    fixedNumber: 1,
                    pk: 'id',
                    extend: {
                        index_url: 'caiwu/record/index?type=1',
                        add_url: 'caiwu/record/add?type=1',
                        edit_url: 'caiwu/record/edit?type=1',
                        del_url: '',
                        multi_url: '',
                        table:'record_shourushiji'
                    },
                    pageSize: Config.pagesize,
                    height: table_height,
                    toolbar: '#toolbarshiji',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'),operate: false,visible: false},
                            {field: 'user_name', title: "操作人",operate: false},
                            {field: 'user_id', title: "选择用户",visible: false,addClass: "selectpage", extend: "data-source='caiwu/record/ajaxUser' data-field='name' data-page-size='20'"},

                            {field: 'cate_bianhao', title: "分类编号",operate: false},

                            {field: 'cate_name', title: "分类名",addClass: "selectpage", extend: "data-source='caiwu/record/ajaxCate?type=1' data-field='cate_name' data-page-size='20'"},
                            {field: 'title', title: "标题",operate: false},
                            {field: 'price', title: "金额",operate: false},
                            {field: 'type', title: "类型",formatter: Controller.api.formatter.setType,operate: false},
                            {field: 'order_bianhao', title: "出库订单编号"},
                            {field: 'remark', title: "备注",operate: false},


                            {field: 'create_time', title: '创建时间',addclass:'datetimerange w200', operate:'RANGE', formatter: Table.api.formatter.datetime},
                            {field: 'update_time', title: '编辑时间', operate:false},
                            {field: 'operate', title: __('Operate'), table: tableshiji, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(tableshiji);


            },
            zhichu:function (){
                // 表格1
                var window_innerheight = window.innerHeight;
                var table_height = window_innerheight-75;
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'caiwu/record/index?type=2',
                    fixedColumns: true,
                    fixedNumber: 1,
                    pk: 'id',
                    extend: {
                        index_url: 'caiwu/record/index?type=2',
                        add_url: 'caiwu/record/add?type=2',
                        edit_url: 'caiwu/record/edit?type=2',
                        del_url: '',
                        multi_url: '',
                        table:'record_shouru'
                    },
                    pageSize: Config.pagesize,
                    height: table_height,
                    toolbar: '#toolbar2',
                    columns: [
                        [
                            {checkbox: true},
                            {field: 'id', title: __('Id'),operate: false,visible: false},
                            {field: 'user_name', title: "操作人",operate: false},
                            {field: 'user_id', title: "选择用户",visible: false,addClass: "selectpage", extend: "data-source='caiwu/record/ajaxUser' data-field='name' data-page-size='20'"},

                            {field: 'cate_bianhao', title: "分类编号",operate: false},


                            {field: 'cate_name', title: "分类名",addClass: "selectpage", extend: "data-source='caiwu/record/ajaxCate?type=2' data-field='cate_name' data-page-size='20'"},
                            {field: 'title', title: "标题",operate: false},
                            {field: 'price', title: "金额",operate: false},
                            {field: 'type', title: "类型",formatter: Controller.api.formatter.setType,operate: false},
                            {field: 'inventory_bianhao', title: "入库编号"},
                            {field: 'remark', title: "备注",operate: false},


                            {field: 'create_time', title: '创建时间',addclass:'datetimerange w200', operate:'RANGE', formatter: Table.api.formatter.datetime},
                            {field: 'update_time', title: '编辑时间', operate:false},
                            {field: 'operate', title: __('Operate'), table: table2, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table2);
                table2.on('post-common-search.bs.table', function (event, table) {
                    var form = $("form", table.$commonsearch);
                    $("input[name='cate_bianhao']", form).addClass("selectpage").data("source", "caiwu/record/ajaxCate?type=2").data("primaryKey", "bianhao").data("field", "cate_name").data("orderBy", "id desc").data('pagination',true).data('page-size',20);
                    Form.events.cxselect(form);
                    Form.events.selectpage(form);
                });
            },
        },

        add: function () {
            Controller.api.bindevent();
        },

        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                setType: function (value, row, index) {
                    if(value == 1){
                        var html = '<span class="label label-success">收入账单</span>';
                    }else{
                        var html = '<span class="label label-default">支出账单</span>';
                    }
                    return html;
                },

            }
        }
    };
    return Controller;
});
